package com.wn.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.material.chip.Chip
import com.wn.common.databinding.LayoutHistorySearchBinding
import com.wn.common.databinding.LayoutHistorySearchChipItemBinding
import com.wn.framework.ext.onClick

/**
 * @Author: wn
 * @Date: 2024/12/11 16:13
 * @Description: 历史搜索
 */
class SearchHistoryView @JvmOverloads constructor(
  context: Context,
  attrs: AttributeSet? = null,
  defStyleAttr: Int = 0,
) : ConstraintLayout(context, attrs, defStyleAttr) {
  private val mKeyWords = mutableListOf<String>()
  private var mBinding: LayoutHistorySearchBinding

  init {
    mBinding = LayoutHistorySearchBinding.inflate(LayoutInflater.from(context), this, true)
    val array =
      context.obtainStyledAttributes(attrs, com.wn.framework.R.styleable.SearchHistoryView)
    val title = array.getString(com.wn.framework.R.styleable.SearchHistoryView_title)
    mBinding.tvSearch.text = title
    array.recycle()
  }

  /**
   * 设置 标签 数据
   */
  fun setHistoryData(histories: MutableList<String>?) {
    if (histories.isNullOrEmpty()) return
    mKeyWords.clear()
    mKeyWords.addAll(histories)
    for (index in 0 until histories.size) {
      var chipItem: Chip
      // 这里也要需要判断一下复用的情况
      val childCount = mBinding.chipGroup.childCount
      if (index < childCount) {
        chipItem = mBinding.chipGroup.getChildAt(index) as Chip // 复用之前的 child
      } else {
        chipItem = createChipItem() // 创建新的 child
        mBinding.chipGroup.addView(chipItem)
      }
      chipItem.text = histories[index]
    }
  }

  /**
   * 创建 Chip
   */
  private fun createChipItem(): Chip {
    val chipItem: Chip = LayoutHistorySearchChipItemBinding.inflate(
      LayoutInflater.from(context),
      mBinding.chipGroup,
      false
    ).root as Chip

    chipItem.isChecked = false // 刚创建的时候是没有选中的
    chipItem.isCheckable = true
    // 还需要去改变每个chip的id，这个id在布局文件的时候是相同的，但是我们添加到chipGroup的时候是不同的
//        chipItem.id = View.generateViewId()
    chipItem.id = mBinding.chipGroup.childCount ?: 0 // 第一次是 0，依次增加
    return chipItem
  }

  /**
   * 需要暴露方法，点击 item 的时候把事件暴露出去
   * 只有 singleSelection=true 时，该监听才有效
   */
  fun setOnCheckChangeListener(callBack: (String) -> Unit) {
    mBinding.chipGroup.setOnCheckedChangeListener { group, checkedId ->
      for (index in 0 until mBinding.chipGroup.childCount) {
        if (mBinding.chipGroup.getChildAt(index)?.id == checkedId) {
          callBack.invoke(mKeyWords[index])
          break
        }
      }
    }
  }

  /**
   * 清除历史记录监听
   */
  fun setOnHistoryClearListener(callBack: (clearSuccess: () -> Unit) -> Unit) {
    mBinding.ivDelete.onClick {
      callBack {
        mBinding.chipGroup.removeAllViews()
        mKeyWords.clear()
      }
    }
  }

  fun getDeleteImageView(): ImageView {
    return mBinding.ivDelete
  }

}